Imports Microsoft.VisualBasic

Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Web

Namespace TressleWorks.Modules.ThermoChart

    Public Class Thermometer

        '---These contants govern placement of the chart components on the target image.
        '   To change the layout of the chart, the constant values will need to be updated appropriately.


        '---Bounds of elipse for curve at top of thermometer.
        Private Const OUTERBAR_WIDTH As Integer = 30
        Private Const OUTERBAR_ARCHEIGHT As Integer = 30

        '---Bounds of elipse for thermometer bulb
        Private Const OUTERBULB_WIDTH As Integer = 55
        Private Const OUTERBULB_HEIGHT As Integer = 45

        '---Bounds of elipse for curve at top of mercury
        Private Const INNERBAR_WIDTH = 18
        Private Const INNERBAR_ARCHEIGHT As Integer = 20

        Private Const INNERBAR_TOPMARGIN = 10                                       '---Amount of space between top of mercury and top of thermometer
        Private Const INNERBAR_BULBMARGIN = 10                                      '---Amount of space between thermometer bulb and mercury bulb.

        '---Bounds of elipse for mercury bulb
        Private Const INNERBULB_WIDTH As Integer = 40
        Private Const INNERBULB_HEIGHT As Integer = 35

        '-------------

        Private miMarginTop As Integer = 20
        Private miMarginBottom As Integer = 20
        Private miMarginLeft As Integer = 20
        Private miMarginRight As Integer = 20

        Private miTitleMargin As Integer = 55
        Private miValueMargin As Integer = 55

        Private miChartTopY As Integer = 0
        Private TitleColor As Color = Color.Gray                                    '---Color of the title text
        Private ActualValueColor As Color = Color.Gray                              '---Color of the actual value text
        Private CurrentValueColor As Color = Color.Gray                             '---Color of the Current value text

        Private miThermoCenter As Integer = 190                                     '---X position of the center of the thermometer within the image
        Private miWidth As Integer = 240                                            '---Width of the image
        Private miHeight As Integer = 400                                           '---Height of the image

        Private miBarWidth As Integer = INNERBAR_WIDTH
        Private miBarHeight As Integer = 0

        Private miInnerBarArchHeight As Integer = INNERBAR_ARCHEIGHT
        Private miOuterBarArchHeight As Integer = OUTERBAR_ARCHEIGHT
        Private miOuterBarArchHeightHalf As Integer = OUTERBAR_ARCHEIGHT / 2
        Private miOuterBarDiff As Integer = OUTERBAR_WIDTH - INNERBAR_WIDTH
        Private miOuterBarWidth As Integer = OUTERBAR_WIDTH

        Private miOuterBulbWidth As Integer = OUTERBULB_WIDTH
        Private miOuterBulbWidthDiff As Integer = OUTERBULB_WIDTH - INNERBULB_WIDTH
        Private miOuterBulbHeight As Integer = OUTERBULB_HEIGHT
        Private miOuterBulbHeightDiff As Integer = OUTERBULB_HEIGHT - INNERBULB_HEIGHT   ' yes the width!!!

        Private miInnerBulbWidth As Integer = INNERBULB_WIDTH
        Private miInnerBulbHeight As Integer = INNERBULB_HEIGHT

        Private miMaxValueY As Integer = 0
        Private miMinValueY As Integer = 0
        Private miValueY As Integer = 0

        Private miMarkerMargin As Integer = 2                                   '---Distance of the axis indicators from the thermometer
        Private miMajorMarkerWidth As Integer = 15                              '---Width of the primary indicator
        Private miMinorMarkerWidth As Integer = 10                              '---Width of the secondary indicator

        Private miChartHeight As Integer = 0                        '---Height of the thermometer

        Private moPen As Pen

        '---The ValueFormatType enum affects the formatting of value text (IE: whether decimal places and the currency sign are displayed.

        Public Sub New()
            Call Initialize()
        End Sub


        Public Sub Initialize()

        End Sub

        Public Sub ShowThermometer(ByVal poInfo As ThermoChartInfo)

            '---Create new image for composite
            Dim oG As Graphics = Nothing
            Dim oImage As Bitmap

            oImage = DetermineShape(poInfo, oG)

            '---Draw thermometer
            Call DrawThermometer(poInfo, oG)

            Call DrawMercury(poInfo, oG)

            Call DisplayTitle(poInfo, oG, poInfo.Title)

            If poInfo.ScaleLocation <> ThermoChartInfo.ScaleLocationType.None Then
                Call DisplayScale(poInfo, oG)
            End If

            If poInfo.ShowValue Then
                Call DisplayCurrentValue(poInfo, oG)
            End If

            'HttpContext.Current.Response.ContentType = "image/Gif"
            'oImage.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Gif)

            If File.Exists(poInfo.Filename) Then
                File.Delete(poInfo.Filename)
            End If

            oImage.Save(poInfo.Filename, ImageFormat.Png)

            oG.Dispose()
            oImage.Dispose()
        End Sub

        Private Function DetermineShape(ByVal poInfo As ThermoChartInfo, ByRef poG As Graphics) As Bitmap

            'can not have rounded tip if bulb not shown
            If poInfo.ShowRoundedTip = True And poInfo.ShowBulb = False Then
                poInfo.ShowRoundedTip = False
            End If

            'can not have rounded tip if gradient over range is enabled -- since rounded tip is from color only not appropriate gradient color
            If poInfo.ShowRoundedTip = True And poInfo.ShowGradientOverRange = True Then
                poInfo.ShowRoundedTip = False
            End If

            miWidth = poInfo.Width
            miHeight = poInfo.Height

            Dim oImage As Bitmap = New Bitmap(miWidth, miHeight, PixelFormat.Format24bppRgb)
            poG = Graphics.FromImage(oImage)

            poG.FillRectangle(New SolidBrush(poInfo.BackGroundSettings.Color), New Rectangle(0, 0, miWidth, miHeight))

            moPen = New Pen(Color.Black)
            If poInfo.BackGroundSettings.ColourCount > 1 Then
                moPen = New Pen(poInfo.BackGroundSettings.Color2)
            End If

            If poInfo.BarWidth = 0 Then
                miBarWidth = INNERBAR_WIDTH
            ElseIf (poInfo.BarWidth < 0 And poInfo.BarWidth >= -100) Then
                Dim dPercent As Single = CSng(Math.Abs(poInfo.BarWidth)) / 100
                miBarWidth = CInt(miWidth * dPercent)
            ElseIf poInfo.BarWidth > 10 Then
                miBarWidth = poInfo.BarWidth
            End If

            miOuterBarWidth = miBarWidth + miOuterBarDiff

            If poInfo.ShowBulb Then
                miInnerBarArchHeight = miBarWidth
                miOuterBarArchHeight = miOuterBarWidth
                miOuterBarArchHeightHalf = CInt(miOuterBarArchHeight / 2)

                miOuterBulbWidth = CInt((miBarWidth * OUTERBULB_WIDTH) / INNERBAR_WIDTH)
                miOuterBulbHeight = CInt((miBarWidth * OUTERBULB_HEIGHT) / INNERBAR_WIDTH)

                miInnerBulbWidth = miOuterBulbWidth - miOuterBulbWidthDiff
                miInnerBulbHeight = miOuterBulbHeight - miOuterBulbHeightDiff
            Else
                miInnerBarArchHeight = 0
                miOuterBarArchHeight = 0
                miOuterBarArchHeightHalf = 0
                miOuterBulbHeight = 0
                miOuterBulbWidth = 0
                miInnerBulbHeight = 0
                miInnerBulbWidth = 0
            End If

            miMarginLeft = poInfo.MarginLeft
            miMarginRight = poInfo.MarginRight
            miMarginTop = poInfo.MarginTop
            miMarginBottom = poInfo.MarginBottom

            miMajorMarkerWidth = poInfo.MajorMarkerWidth
            miMinorMarkerWidth = poInfo.MinorMarkerWidth
            miMarkerMargin = poInfo.MarkerMargin

            miThermoCenter = poInfo.Width / 2 + poInfo.CenterLineAdjust                     '240 / 2 = 120 + 50 = 190

            If poInfo.Title.Length = 0 Then
                miTitleMargin = 0
            End If

            miValueMargin = 0

            miChartTopY = miMarginTop + miTitleMargin

            miChartHeight = _
                miHeight - (miChartTopY + miOuterBarArchHeightHalf + miOuterBulbHeight + miValueMargin + INNERBAR_TOPMARGIN)         '---Height of the thermometer

            If poInfo.ShowRoundedTip Then
                miMaxValueY = miChartTopY
            Else
                miMaxValueY = miChartTopY + miOuterBarArchHeightHalf
            End If

            miMinValueY = miHeight - (miOuterBulbHeight + miValueMargin + INNERBAR_TOPMARGIN)
            miBarHeight = miMinValueY - miMaxValueY
            miValueY = miMinValueY - CInt(miBarHeight * poInfo.CurrentPercent)
            If miValueY < miMaxValueY Then
                miValueY = miMaxValueY
                'Debug.Print("here")
            End If

            Return oImage
        End Function

        Private Sub DrawThermometer(ByVal poInfo As ThermoChartInfo, ByRef oGraphic As Graphics)

            Dim iCenterX As Integer = miThermoCenter
            Dim iTopY As Integer = miChartTopY
            Dim iBottomY As Integer = miHeight - miMarginBottom

            Dim iOuterX_Left As Integer = iCenterX - (miOuterBarWidth / 2)
            Dim iOuterX_Right As Integer = iCenterX + (miOuterBarWidth / 2)

            Dim iOuterYArc_Top As Integer = iTopY
            Dim iOuterBar_TopY As Integer = iTopY + miOuterBarArchHeightHalf

            '---Draw lines on each side
            oGraphic.DrawLine(moPen, iOuterX_Left, iOuterBar_TopY, iOuterX_Left, miMinValueY + 1)
            oGraphic.DrawLine(moPen, iOuterX_Right, iOuterBar_TopY, iOuterX_Right, miMinValueY + 1)

            If poInfo.ShowBulb Then
                '---Draw top arc
                oGraphic.DrawArc(moPen, iOuterX_Left, iTopY, miOuterBarWidth, miOuterBarArchHeight, 180, 180)
                '---Draw bulb at bottom
                Dim iBulb_Left As Integer = iCenterX - miOuterBulbWidth / 2
                Dim iBulb_Top As Integer = miMinValueY - 2

                If miOuterBulbHeight > 100 Then
                    iBulb_Top -= CInt(miOuterBulbHeight / 70)
                End If
                oGraphic.DrawArc(moPen, _
                            iBulb_Left, iBulb_Top, _
                           miOuterBulbWidth, miOuterBulbHeight, 0, 360)

                'Draw a rectangle of white to remove top of cirle
                Dim oBrush As Brush = New SolidBrush(poInfo.BackGroundSettings.Color)
                oGraphic.FillRectangle(oBrush, iOuterX_Left + 1, iOuterBar_TopY + 20, (iOuterX_Right - iOuterX_Left) - 1, miBarHeight)
            Else

                oGraphic.DrawLine(moPen, iOuterX_Left, iOuterBar_TopY - 1, iOuterX_Right, iOuterBar_TopY - 1)
                oGraphic.DrawLine(moPen, iOuterX_Left, miMinValueY + 1, iOuterX_Right, miMinValueY + 1)

            End If

        End Sub

        Private Sub DrawMercury(ByVal poInfo As ThermoChartInfo, _
                    ByRef poGraphic As Graphics)

            'Mecury consists of 3 layers -- shadow displayed firsts
            'then primary color, followed by highlights

            If poInfo.ShowShadow Then
                Call DrawMercuryShadow(poInfo, poGraphic, 2, 4)
            End If

            If poInfo.ShowMain Then
                Call DrawMercuryMain(poInfo, poGraphic)
            End If
            'Call DrawMercuryHiLite(poInfo, poGraphic, pdFillPercent)

            If poInfo.ShowLines Then
                '---CenterLine (testing)
                poGraphic.DrawLine(New Pen(Color.Red), miThermoCenter, 0, miThermoCenter, miHeight)

                '---value (testing)
                Dim iy As Integer = miValueY
                poGraphic.DrawLine(New Pen(Color.Red), 0, iy, miWidth, iy)

                '---0 value (testing)
                iy = miMinValueY
                poGraphic.DrawLine(New Pen(Color.Red), 0, iy, miWidth, iy)

                '---100 value (testing)
                iy = miMaxValueY
                poGraphic.DrawLine(New Pen(Color.Green), 0, iy, miWidth, iy)
            End If

        End Sub

        Private Sub DrawMercuryShadow(ByVal poInfo As ThermoChartInfo, _
            ByRef oGraphic As Graphics, _
            ByVal iVAdj As Integer, ByVal iHAdj As Integer)

            Dim iFillTop As Integer = miValueY
            Dim iFillHeight As Integer = miMinValueY - miValueY

            Dim iCenterX As Integer = miThermoCenter

            Dim iInnerX_Left As Integer = iCenterX - (miBarWidth / 2)
            Dim iInnerX_Right As Integer = iCenterX + (miBarWidth / 2)

            Dim iInnerYArc_Top As Integer = iFillTop
            Dim iInnerYArc_Bottom As Integer = iFillTop + (miInnerBarArchHeight / 2)

            '---For bulb at bottom
            Dim iBulb_Left As Integer = iCenterX - miInnerBulbWidth / 2
            Dim iBulb_Top As Integer = miMinValueY

            Dim iBar_Width As Integer = miBarWidth
            Dim iBar_Archeight As Integer = miInnerBarArchHeight

            Dim oBrush1 As Brush

            Dim iX As Integer = 0
            Dim iY As Integer = 0
            Dim iW As Integer = 0
            Dim iH As Integer = 0

            'each section consists of 3 parts
            '1 - top arc, 2 - bar, 3 - bulb

            iBar_Width -= 1
            iFillTop += iVAdj
            iInnerX_Left += iHAdj

            '---Draw Bar
            oBrush1 = New SolidBrush(poInfo.MercuryShadowSettings.Color)

            If poInfo.ShowRoundedTip Then
                iX = iInnerX_Left
                iY = iFillTop
                iW = iBar_Width
                iH = iBar_Archeight
                oGraphic.FillEllipse(oBrush1, iX, iY, iW, iH)
            End If

            '---Draw bulb
            If poInfo.ShowBulb Then
                iX = iBulb_Left + 3 + iHAdj
                iY = iBulb_Top + 3 + iHAdj
                iW = miInnerBulbWidth - 5
                iH = miInnerBulbHeight - 5
                oGraphic.FillEllipse(oBrush1, iX, iY, iW, iH)

                'if roundedtip then start of bar is lower
                iFillHeight += CInt(miInnerBulbHeight / 2)       'add a bit to run into blub
            End If

            If poInfo.ShowRoundedTip Then
                iX = iInnerX_Left
                iY = CInt(iFillTop + (iBar_Archeight / 2))
                iW = iBar_Width
                iH = iFillHeight
                oGraphic.FillRectangle(oBrush1, iX, iY, iW, iH)
            Else
                iX = iInnerX_Left
                iY = iFillTop - 1
                iW = iBar_Width
                iH = iFillHeight
                oGraphic.FillRectangle(oBrush1, iX, iY, iW, iH)
            End If

        End Sub

        Private Sub DrawMercuryMain(ByVal poInfo As ThermoChartInfo, _
            ByRef oGraphic As Graphics)

            Dim iFillTop As Integer = miValueY
            Dim iFillHeight As Integer = miMinValueY - miValueY

            Dim iCenterX As Integer = miThermoCenter

            Dim iInnerX_Left As Integer = iCenterX - (miBarWidth / 2)
            Dim iInnerX_Right As Integer = iCenterX + (miBarWidth / 2)

            Dim iInnerYArc_Top As Integer = iFillTop
            Dim iInnerYArc_Bottom As Integer = iFillTop + (miInnerBarArchHeight / 2)

            '---For bulb at bottom
            Dim iBulb_Left As Integer = iCenterX - miInnerBulbWidth / 2
            Dim iBulb_Top As Integer = miMinValueY

            Dim iBar_Width As Integer = miBarWidth
            Dim iBar_Height As Integer = miBarHeight
            Dim iBar_Archeight As Integer = miInnerBarArchHeight

            Dim oBrush1 As Brush
            Dim oBrush2 As Brush

            Dim iX As Integer = 0
            Dim iY As Integer = 0
            Dim iW As Integer = 0
            Dim iH As Integer = 0

            'each section consists of 3 parts
            '1 - top arc, 2 - bar, 3 - bulb

            '---Draw Bar
            oBrush1 = New SolidBrush(poInfo.MercuryColourSettings.Color)     'Color.GreenYellow)
            If poInfo.MercuryColourSettings.ColourCount > 1 Then
                oBrush2 = New SolidBrush(poInfo.MercuryColourSettings.Color2)
            Else
                oBrush2 = oBrush1
            End If

            If poInfo.ShowRoundedTip And Not poInfo.ShowGradientOverRange Then
                'Create tip with first color
                iX = iInnerX_Left
                iY = iFillTop
                iW = iBar_Width
                iH = iBar_Archeight
                oGraphic.FillEllipse(oBrush1, iX, iY, iW, iH)
            End If

            '---Draw bulb
            If poInfo.ShowBulb Then
                'Create bulb with second color 
                iX = iBulb_Left + 3
                iY = iBulb_Top + 3
                iW = miInnerBulbWidth - 5
                iH = miInnerBulbHeight - 5
                oGraphic.FillEllipse(oBrush2, iX, iY, iW, iH)

                iFillHeight += CInt(miInnerBulbHeight / 4)         'add a bit to run into blub
                iBar_Height += CInt(miInnerBulbHeight / 4)

            End If

            '---Draw Bar
            Dim oRect As Rectangle

            'Dim oBrush As Brush
            If poInfo.MercuryColourSettings.ColourCount > 1 Then

                If poInfo.ShowGradientOverRange Then
                    'Rounded Tip will be false
                   
                    'gradient covers entire shaft lenght, so cover up unwanted.
                    iX = iInnerX_Left
                    iW = iBar_Width

                    'display gradient over entire range

                    iY = miMaxValueY
                    iH = iBar_Height

                    oRect = New Rectangle(iInnerX_Left, iY - 1, iBar_Width, iBar_Height + 1)
                    oBrush2 = New Drawing.Drawing2D.LinearGradientBrush(oRect, poInfo.MercuryColourSettings.Color, poInfo.MercuryColourSettings.Color2, Drawing2D.LinearGradientMode.Vertical)
                    oGraphic.FillRectangle(oBrush2, iX, iY, iW, iH)

                    'cover up excess 
                    iH = miValueY - iY
                    oBrush2 = New SolidBrush(poInfo.BackGroundSettings.Color)
                    oGraphic.FillRectangle(oBrush2, iX, iY, iW, iH)
                Else
                    oRect = New Rectangle(iInnerX_Left, iFillTop - 1, iBar_Width, iFillHeight + 1)
                    oBrush2 = New Drawing.Drawing2D.LinearGradientBrush(oRect, poInfo.MercuryColourSettings.Color, poInfo.MercuryColourSettings.Color2, Drawing2D.LinearGradientMode.Vertical)

                    'gradient covers only value length
                    iX = iInnerX_Left
                    iW = iBar_Width
                    If poInfo.ShowRoundedTip Then
                        iY = CInt(iFillTop + (iBar_Archeight / 2))
                        iH = iFillHeight - CInt((iBar_Archeight / 2)) - 1
                    Else
                        iY = iFillTop
                        iH = iFillHeight
                    End If
                    oGraphic.FillRectangle(oBrush2, iX, iY, iW, iH)
                End If

            Else
                'single color
                iX = iInnerX_Left
                iW = iBar_Width
                If poInfo.ShowRoundedTip Then
                    iY = CInt(iFillTop + (iBar_Archeight / 2))
                    iH = iFillHeight - CInt((iBar_Archeight / 2)) - 1
                Else
                    iY = iFillTop
                    iH = iFillHeight
                End If
                oGraphic.FillRectangle(oBrush2, iX, iY, iW, iH)
            End If

        End Sub

        Private Sub DrawMercuryHiLite(ByVal poInfo As ThermoChartInfo, _
           ByRef oGraphic As Graphics, _
           ByRef dFillPercent As Decimal)

        End Sub

        Private Sub DisplayTitle(ByVal poInfo As ThermoChartInfo, _
            ByRef oGraphic As Graphics, _
            ByVal sText As String)

            If sText.Length = 0 Then
                Exit Sub
            End If

            Dim oBrush As New System.Drawing.SolidBrush(poInfo.TitleFontSettings.Color)
            Dim oFont As Font = poInfo.TitleFontSettings.AsFont

            Dim oSize As New SizeF
            oSize = oGraphic.MeasureString(sText, oFont)

            Dim iX As Integer = miWidth / 2 - oSize.Width / 2
            Dim iY As Integer = miMarginTop
            oGraphic.DrawString(sText, _
                                  oFont, _
                                  oBrush, _
                                  iX, iY, _
                                  System.Drawing.StringFormat.GenericTypographic)

        End Sub


        Private Sub DisplayCurrentValue(ByVal poInfo As ThermoChartInfo, _
                   ByRef oGraphic As Graphics)

            Dim sText As String = poInfo.CurrentValueToText()
            If poInfo.ShowValueAsPercent Then
                If poInfo.ValueFormat = ThermoChartInfo.ValueFormatType.UserDefined Then
                    sText = poInfo.ValueToText(poInfo.CurrentPercent * 100.0, poInfo.ValueFormatString)
                Else
                    sText = poInfo.ValueToText(poInfo.CurrentPercent * 100.0, "{0:0\%}")
                End If
            End If

            Dim oBrush As New System.Drawing.SolidBrush(poInfo.ValueFontSettings.Color)
            Dim oFont As Font = poInfo.ValueFontSettings.AsFont

            Dim oSubPen As Pen = moPen
            If poInfo.BackGroundSettings.ColourCount > 2 Then
                oSubPen = New Pen(poInfo.BackGroundSettings.Color3)
            End If

            Dim oSize As New SizeF
            oSize = oGraphic.MeasureString(sText, oFont)

            Dim iX As Integer = miMarginLeft
            Dim iY As Integer = (miHeight - miMarginBottom) - oSize.Height
            'oGraphic.DrawString(sText, _
            '                      oFont, _
            '                      oBrush, _
            '                      iX, _
            '                      iY, _
            '                      System.Drawing.StringFormat.GenericTypographic)

            Select Case poInfo.ScaleLocation
                Case ThermoChartInfo.ScaleLocationType.Left, ThermoChartInfo.ScaleLocationType.None
                    'scale on left so value on right
                    iX = miThermoCenter + (miOuterBarWidth / 2) + miMarkerMargin
                    oGraphic.DrawLine(oSubPen, iX, miValueY, iX + miMinorMarkerWidth, miValueY)

                    iX = iX + miMinorMarkerWidth + miMarkerMargin
                    iY = miValueY - (oSize.Height / 2)
                    oGraphic.DrawString(sText, _
                                          oFont, _
                                          oBrush, _
                                          iX, _
                                          iY, _
                                          System.Drawing.StringFormat.GenericTypographic)

                Case ThermoChartInfo.ScaleLocationType.Right
                    'scale on right so value on Left
                    iX = miThermoCenter - (miOuterBarWidth / 2) - miMarkerMargin
                    oGraphic.DrawLine(oSubPen, iX - miMinorMarkerWidth, miValueY, iX, miValueY)

                    iX = iX - miMajorMarkerWidth - CInt(oSize.Width)
                    iY = miValueY - (oSize.Height / 2)
                    oGraphic.DrawString(sText, _
                                          oFont, _
                                          oBrush, _
                                          iX, _
                                          iY, _
                                          System.Drawing.StringFormat.GenericTypographic)

            End Select


        End Sub


        Private Sub DisplayScale(ByVal poInfo As ThermoChartInfo, ByRef oGraphic As Graphics)

            If poInfo.ScaleLocation = ThermoChartInfo.ScaleLocationType.None Then
                Exit Sub
            End If

            Dim oSubPen As Pen
            Dim dMin As Decimal = poInfo.MinValue
            Dim dMax As Decimal = poInfo.MaxValue
            Dim iCount As Integer = poInfo.MajorMarkerCount
            Dim iShowMarkerInterval As Integer = poInfo.MinorMarkerCount
            Dim iType As ThermoChartInfo.ValueFormatType = poInfo.ValueFormat

            Dim iXLeft1 As Integer = miThermoCenter - miOuterBarWidth / 2 - miMajorMarkerWidth
            Dim iXLeft2 As Integer = miThermoCenter - miOuterBarWidth / 2 - miMinorMarkerWidth
            Dim iXLeftMargin As Integer = miThermoCenter - miOuterBarWidth / 2 - miMarkerMargin

            Dim iXRight1 As Integer = miThermoCenter + miOuterBarWidth / 2 + miMajorMarkerWidth
            Dim iXRight2 As Integer = miThermoCenter + miOuterBarWidth / 2 + miMinorMarkerWidth
            Dim iXRightMargin As Integer = miThermoCenter + miOuterBarWidth / 2 + miMarkerMargin

            Dim dSpace As Decimal = (miMinValueY - miMaxValueY) / iCount
            Dim dSubSpace As Decimal = dSpace / (iShowMarkerInterval + 1)

            Dim dValue As Decimal = (dMax - dMin) / iCount

            oSubPen = moPen
            If poInfo.BackGroundSettings.ColourCount > 2 Then
                oSubPen = New Pen(poInfo.BackGroundSettings.Color3)
            End If

            Dim dYTop As Decimal = miMaxValueY
            Dim iYtop As Integer
            Dim dDisplay As Decimal

            For iIndicator As Integer = 0 To iCount
                iYtop = CInt(dYTop)
                dDisplay = dMax - dValue * iIndicator

                Select Case poInfo.ScaleLocation

                    Case ThermoChartInfo.ScaleLocationType.Left
                        'if showing bulb and rounded top and display max value - drawline from centerline
                        If poInfo.ShowRoundedTip And dDisplay = poInfo.MaxValue Then
                            oGraphic.DrawLine(moPen, iXLeft1, iYtop - 1, miThermoCenter - miMarkerMargin, iYtop - 1)
                        Else
                            oGraphic.DrawLine(moPen, iXLeft1, iYtop, iXLeftMargin, iYtop)
                        End If

                        ShowAxisText(poInfo, oGraphic, dDisplay, iXLeft1, iYtop)

                        If iIndicator < iCount Then
                            For iSubIndicator As Integer = 1 To iShowMarkerInterval
                                oGraphic.DrawLine(oSubPen, iXLeft2, CInt(iYtop) + dSubSpace * iSubIndicator, iXLeftMargin, CInt(iYtop) + dSubSpace * iSubIndicator)
                            Next
                        End If

                    Case ThermoChartInfo.ScaleLocationType.Right
                        'if showing bulb and rounded top and display max value - drawline from centerline
                        If poInfo.ShowRoundedTip And dDisplay = poInfo.MaxValue Then
                            oGraphic.DrawLine(moPen, miThermoCenter + miMarkerMargin, iYtop - 1, iXRight1, iYtop - 1)
                        Else
                            oGraphic.DrawLine(moPen, iXRightMargin, iYtop, iXRight1, iYtop)
                        End If

                        ShowAxisText(poInfo, oGraphic, dDisplay, iXRight1, iYtop)

                        If iIndicator < iCount Then
                            For iSubIndicator As Integer = 1 To iShowMarkerInterval
                                oGraphic.DrawLine(oSubPen, iXRightMargin, CInt(iYtop) + dSubSpace * iSubIndicator, iXRight2, CInt(iYtop) + dSubSpace * iSubIndicator)
                            Next
                        End If

                End Select

                dYTop = dYTop + dSpace
            Next
        End Sub



        Private Sub ShowAxisText(ByVal poInfo As ThermoChartInfo, _
                        ByRef oGraphic As Graphics, _
                        ByRef dValue As Decimal, _
                        ByRef iXRightPos As Integer, _
                        ByRef iYCenter As Integer)

            Dim sText As String = poInfo.ValueToText(dValue, poInfo.ScaleFormatString)

            Dim oBrush As New System.Drawing.SolidBrush(poInfo.ScaleFontSettings.Color)
            Dim oFont As Font = poInfo.ScaleFontSettings.AsFont

            Dim oSize As New SizeF
            oSize = oGraphic.MeasureString(sText.Trim, oFont)

            Dim iX As Integer = iXRightPos - oSize.Width
            If poInfo.ScaleLocation = ThermoChartInfo.ScaleLocationType.Right Then
                iX = iXRightPos
                sText = " " & sText
            End If

            Dim iY As Integer = iYCenter - oSize.Height / 2

            oGraphic.DrawString(sText, _
                          oFont, _
                          oBrush, _
                          iX, _
                          iY, _
                          System.Drawing.StringFormat.GenericTypographic)
        End Sub


    End Class

End Namespace